package com.ruoyi.system.controller;

import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.system.domain.Preferential;
import com.ruoyi.system.service.IPreferentialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 优惠卷Controller
 * 
 * @author ruoyi
 * @date 2022-12-25
 */
@RestController
@RequestMapping("/event/preferential")
public class PreferentialController extends BaseController
{
    @Autowired
    private IPreferentialService preferentialService;

    /**
     * 查询优惠卷列表
     */
    @RequiresPermissions("system:preferential:list")
    @GetMapping("/list")
    public TableDataInfo list(Preferential preferential)
    {
        startPage();
        List<Preferential> list = preferentialService.selectPreferentialList(preferential);
        return getDataTable(list);
    }

    /**
     * 导出优惠卷列表
     */
    @RequiresPermissions("system:preferential:export")
    @Log(title = "优惠卷", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Preferential preferential)
    {
        List<Preferential> list = preferentialService.selectPreferentialList(preferential);
        ExcelUtil<Preferential> util = new ExcelUtil<Preferential>(Preferential.class);
        util.exportExcel(response, list, "优惠卷数据");
    }

    /**
     * 获取优惠卷详细信息
     */
    @RequiresPermissions("system:preferential:query")
    @GetMapping(value = "/query/{pid}")
    public AjaxResult getInfo(@PathVariable("pid") Long pid)
    {
        return AjaxResult.success(preferentialService.selectPreferentialByPid(pid));
    }

    /**
     * 新增优惠卷
     */
    @RequiresPermissions("system:preferential:add")
    @Log(title = "优惠卷", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody Preferential preferential)
    {
        return toAjax(preferentialService.insertPreferential(preferential));
    }

    /**
     * 修改优惠卷
     */
    @RequiresPermissions("system:preferential:edit")
    @Log(title = "优惠卷", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody Preferential preferential)
    {
        return toAjax(preferentialService.updatePreferential(preferential));
    }

    /**
     * 删除优惠卷
     */
    @RequiresPermissions("system:preferential:remove")
    @Log(title = "优惠卷", businessType = BusinessType.DELETE)
	@DeleteMapping("remove/{pids}")
    public AjaxResult remove(@PathVariable Long[] pids)
    {
        return toAjax(preferentialService.deletePreferentialByPids(pids));
    }
}
